package com.example.wm_mbp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wm_mbp.common.R;
import com.example.wm_mbp.model.Dish;
import com.example.wm_mbp.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * ClassName: DishController
 * Package: com.example.wm_mbp.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-03-15
 * Time: 22:54
 */

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    //GET
    //http://localhost:8080/dish/page?page=1&pageSize=10&name=号
    //菜品管理的分页查询
    @GetMapping("/page")
    public R pageDish(Integer page,Integer pageSize,String name){

        //构造分页构造器
        Page<Dish> page1 = new Page<>(page,pageSize);

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name),Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getId);
        //执行查询 查的数据不用返回给page1   page内部 就会将数据封装 这样就少写了创建IPage的 过程
        dishService.page(page1,queryWrapper);

        return R.success(page1);

    }

}
